import { createRouter, createWebHistory } from 'vue-router';
import store from '@/store';
import Login from '@/views/Login.vue';

const routes = [
  {
    path: '/',
    redirect: 'login',
  },
  {
    path: '/login',
    name: 'login',
    component: Login,
  },
  {
    path: '/backstage',
    name: '后台管理系统',
    component: () => import(/* webpackChunkName: "about" */ '@/views/Index.vue'),
    children: [
      {
        path: '',
        name: '后台主界面',
        component: () => import(/* webpackChunkName: "about" */ '@/views/Main.vue'),
      },
      {
        path: 'DepartmentManage',
        name: '部门管理',
        component: () => import('@/views/page/departmentmanage/Index.vue'),
      },
      {
        path: 'MenuManage',
        name: '权限管理',
        component: () => import('@/views/page/permissionmanage/Index.vue'),
      },
      {
        path: 'RoleManage',
        name: '角色管理',
        component: () => import('@/views/page/rolemanage/Index.vue'),
      },
      {
        path: 'UserManage',
        name: '账户管理',
        component: () => import('@/views/page/usermanage/Index.vue'),
      },
      {
        path: '404',
        name: '404页面',
        component: () => import('@/components/404.vue'),
      },
    ],
  },
  { path: '/:pathMatch(.*)', redirect: '/backstage/404' },
];
const router = createRouter({
  // 用了history模式后,可以用两种方式操作：
  // 1.在服务器或本机测试时可以使用nginx服务来实现
  // 2.如果是单纯本机测试打包后的文件，可以在本机全局安装插件：npm install serve -g。然后用serve -s 【目录】来运行。例如本例http://localhost:3000
  history: createWebHistory(process.env.BASE_URL),
  routes,
});
// 拼接生成路由数据
const getRoutesData = (permissions) => {
  permissions.forEach((permission) => {
    routes.push({
      redirect: '',
      path: permission.route,
      name: permission.component,
      component: () => import(permission.path),
    });
  });
  return routes;
};
router.beforeEach((to, from, next) => {
  // 获取后端的权限数据
  store.dispatch('getRoutes');
  console.log(store.state.routes);
  // 获取路由数据
  let data = getRoutesData(store.state.routes);
  console.log(`routes:${routes}`);
  console.log(`data:${data}`);
  // 动态添加路由
  // data.forEach((routeRaw:RouteRecordRaw) => {
  //   router.addRoute(routeRaw);
  // });
  // 继续执行下一个步骤
  next();
});
export default router;
// # sourceMappingURL=index.js.map
